/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  推荐功能(需嵌套模板使用)
 * @author  wangxin | xinwang200110@sohu-inc.com
 * @version 1.0 | 2013-04-17
 * @param
 * @example
 */
$require('mod.cPrompt');

LJS.define('cpt.recommend', function(ljs){
    var that = {};
    function sendRequest(data){
        $.ajax({
            url: '/zhuanti/tuijian',
            //url: 'test1.txt',
            data: {
                tuijian_id: that.id,
                place_id: data
            },
            type: 'POST',
            dataType: 'json',
            cache: false,
            success: function(json){
                that.layer.del();
                that.layer = null;
                if (json.status == '100000') {
                    ljs.mod.cAlert({
                        tpl: '<div class="inforPop"><div class="title"><span class="btnExit"><a href="javascript:void(0);"></a></span></div><div class="txtGod"><span>推荐成功!</span></div></div>',
                        caClose: false,
						time:1000,
                        btnTxt: '确定'
                    });
                    $('span[box-id="'+that.id+'"]').html('已推荐');
					$('span[box-id-del="'+that.id+'"]').html('');
                }else if(json.status=='100003'){
                	window.location.href = json.content.url;
                }
                else {
                    alert(json.msg);
                }
            },
            error: function(){
                that.layer.del();
                that.layer = null;
                alert('数据加载失败');
            }
        });
    }
    function addEvt(){
        that.box.delegate('a[button-type="recom"]', 'click', function(e){
            e = e || window.event;
            var target = e.target || e.srcElement;
            that.id = target.getAttribute('button-id');
			that.name = target.getAttribute('button-name')
            $.ajax({
				url: '/zhuanti/get_tuijian',
                //url: 'test.txt',
                type: 'get',
                dataType: 'json',
                cache: false,
                success: function(json){
                    if (json.status == '100000') {
                        var dataArr = json.content;var op;
                        var str;
                        var flag = false;
//                        console.log(dataArr);
                        for(i=0;i<dataArr.length;i++){
                        		op += '<option value ="' + dataArr[i].id + '">' + dataArr[i].title + '</option>';
                        }
                        str = '<div class="infor inforNew inforJoin"><div class="title"><span class="txt">信息提示</span><span class="btnExit"><a href="javascript:void(0):" event_type="del"></a></span></div><div class="fillList"><ul><li class="fillTxt"><div style="width:60px;" class="tit">推荐到：</div><select style="color:#CCCCCC;float:left;font-size:12px;height:24px;line-height:24px;width:240px;margin-top:4px;" id="recommendPlace" data="0">'+op+'</select></li></ul></div><div style="padding-left:75px; padding-top:30px;" class="btnEn"><a class="btn-et" href="javascript:void(0);" event_type="ok"><span>确定</span></a><a class="btn-ex" href="javascript:void(0);" event_type="del"><span>取消</span></a></div></div>';
                        that.layer = ljs.mod.cPrompt({
                            tpl: str,//'<div class="infor inforNew inforJoin"><div class="title"><span class="txt">信息提示</span><span class="btnExit"><a href="javascript:void(0):" event_type="del"></a></span></div><div class="fillList"><ul><li class="fillTxt"><div style="width:60px;" class="tit">推荐到：</div><select style="color:#CCCCCC;float:left;font-size:12px;height:24px;line-height:24px;width:240px;margin-top:4px;" id="recommendPlace" data="0"><option value ="' + dataArr[0].name + '">' + dataArr[0].name + '</option><option value ="' + dataArr[1].name + '">' + dataArr[1].name + '</option><option value ="' + dataArr[2].name + '">' + dataArr[2].name + '</option></select></li></ul></div><div style="padding-left:75px; padding-top:30px;" class="btnEn"><a class="btn-et" href="javascript:void(0);" event_type="ok"><span>确定</span></a><a class="btn-ex" href="javascript:void(0);" event_type="del"><span>取消</span></a></div></div>',
                            caClose: true,
                            txt: '',
                            btnTxt: '确定',
                            fun: function(){
                                var data = dataArr[$('#recommendPlace')[0].getAttribute('data')].id;
                                sendRequest(data);
                            }
                        });
                        var select = $('#recommendPlace')[0];
                        $('#recommendPlace').on('change', function(e){
                            select.setAttribute('data', select.selectedIndex);
                        });
                    }
                    else {
                        alert(json.msg);
                    }
                },
                error: function(){
                    alert('数据加载失败');
                }
            });
        });
    }
    that.init = function(){
        that.box = $('#list');
        addEvt();
    }
    return that;
});
